<template>
  <div class="actUserDetailBox">
    <ElCard class="art-table-card" shadow="never">
      <div class="actUser-box">
        <el-row :gutter="24">
          <el-col :span="8">
            <div class="actUser-left">
              <div class="act-box">
                <el-descriptions title="兑换点信息" :column="1" :border="true" label-width="120px">
                  <el-descriptions-item label="名称">{{ actInfo.restName }}</el-descriptions-item>
                  <el-descriptions-item label="地址">{{
                    actInfo.restAddress
                  }}</el-descriptions-item>
                  <el-descriptions-item label="图片">
                    <div v-if="actInfo?.restPhoto?.length > 0" class="flex gap-2">
                      <ElImage
                        :src="actInfo.restPhoto[0].url"
                        :preview-src-list="actInfo.restPhoto.map((item: any) => item.url)"
                        preview-teleported
                        fit="cover"
                        class="w-10 h-10"
                      />
                      <span v-if="actInfo.restPhoto.length > 1" class="text-xs text-gray-500 ml-1">
                        +{{ actInfo.restPhoto.length - 1 }}张
                      </span>
                    </div>
                    <span v-else class="no-image">暂无图片</span>
                  </el-descriptions-item>
                  <el-descriptions-item label="联系电话">{{
                    actInfo.restPhone
                  }}</el-descriptions-item>
                  <el-descriptions-item label="开放时间">{{
                    actInfo.restLongOpenFlag === 2 ? '24小时' : actInfo.restOpenTime
                  }}</el-descriptions-item>
                  <el-descriptions-item label="上架状态">{{
                    actInfo.restUpFlag === 1 ? '隐藏' : '显示'
                  }}</el-descriptions-item>
                </el-descriptions>
              </div>
            </div>
          </el-col>
          <el-col :span="15">
            <div class="actUser-right">
              <div class="act-user-box">
                <el-descriptions title="商品信息"></el-descriptions>
                <!-- 数据表格 -->
                <ElButton type="primary" @click="addGoodsEvent"> 添加商品 </ElButton>
                <el-table :data="goodsList" :border="true" style="width: 100%; margin-top: 10px">
                  <el-table-column prop="id" label="ID" width="80">
                    <template #default="scope">
                      {{ scope.row.scoGoodsVo.id }}
                    </template>
                  </el-table-column>
                  <el-table-column prop="scoreName" label="名称" width="150">
                    <template #default="scope">
                      <div
                        style="color: blue; cursor: pointer"
                        @click="lookGoodsEvent(scope.row.scoGoodsVo.id)"
                        >{{ scope.row.scoGoodsVo.scoreName }}</div
                      >
                    </template>
                  </el-table-column>
                  <el-table-column label="商品图片" width="120">
                    <template #default="scope">
                      <div v-if="scope.row.scoGoodsVo.scorePhoto" class="image-preview">
                        <ElImage
                          :src="scope.row.scoGoodsVo.scorePhoto"
                          :preview-src-list="[scope.row.scoGoodsVo.scorePhoto]"
                          fit="cover"
                          class="product-img"
                          :preview-teleported="true"
                        />
                      </div>
                      <span v-else class="text-gray-400">无图片</span>
                    </template>
                  </el-table-column>
                  <el-table-column prop="scoreCnt" label="兑换积分" width="100">
                    <template #default="scope">
                      {{ scope.row.scoGoodsVo.scoreCnt }}
                    </template>
                  </el-table-column>
                  <el-table-column prop="shopStockNum" label="库存" width="80"> </el-table-column>
                  <el-table-column prop="scoreUpFlag" label="上架状态" width="90">
                    <template #default="scope">
                      {{
                        scope.row.shopUpFlag === 1
                          ? '下架'
                          : scope.row.shopUpFlag === 2
                            ? '上架'
                            : '未知'
                      }}
                    </template>
                  </el-table-column>
                  <el-table-column fixed="right" label="操作" min-width="80">
                    <template #default="scope">
                      <ArtButtonTable type="more" @click="handleOperation(scope.row)"
                        >操作
                      </ArtButtonTable>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </ElCard>
  </div>
  <SelectGoodsDialog v-model:visible="selectGoodsVisible" @confirm="handleGoodsSelect" />
  <operation
    v-model="editInventoryDialogVisible"
    :act-data="currentAreaData"
    @success="handleDialogSuccess"
  />
  <LookProductDetails
    :dialogVisible="lookProductDetailsVisible"
    :act-data="productDetails"
    @closeEvent="((lookProductDetailsVisible = false), (productDetails = null))"
  />
</template>

<script setup lang="ts">
  import SelectGoodsDialog from '@/components/core/views/goodsList/index.vue'
  import operation from './modules/operation.vue'
  import LookProductDetails from '../integralGoods/modules/lookProductDetails.vue'
  import { useRoute } from 'vue-router'
  import { ElMessage } from 'element-plus'
  const route = useRoute()
  import { ref, onMounted } from 'vue'
  import {
    scoGoodsShopPageDataUsingPost,
    scoGoodsShopInStockUsingPost
  } from '@/api/scOjifenshangpinduihuandi'
  import { geoRestGetDataUsingPost, scoGoodsGetDataUsingPost } from '@/api'
  onMounted(() => {
    const id = route.query.id
    if (id) {
      getActInfo(id) //驿站获取数据
      getUserList(id) //获取商品数量
    }
  })
  const goodsList = ref<any[]>([])
  const actInfo = ref<any>({})
  // 获取驿站信息
  const getActInfo = async (id) => {
    const res = await geoRestGetDataUsingPost({
      body: {
        id: id
      }
    })
    actInfo.value = res
    if (actInfo.value.restPhoto && actInfo.value.restPhoto.length > 0) {
      actInfo.value.restPhoto = JSON.parse(actInfo.value.restPhoto) || '[]'
    } else {
      actInfo.value.restPhoto = []
    }
  }

  // 获取商品列表
  const getUserList = async (id) => {
    try {
      const res = await scoGoodsShopPageDataUsingPost({
        body: {
          shopId: id,
          current: 1,
          size: 100
        }
      })
      goodsList.value = res.list || []
    } catch (error) {
      console.log(error)
      console.error('获取商品列表失败:', error)
    }
  }

  const selectGoodsVisible = ref(false)
  // 选中商品回调
  const handleGoodsSelect = async (rows) => {
    console.log('选择的商品:', rows)
    try {
      await scoGoodsShopInStockUsingPost({
        body: {
          shopId: route.query.id,
          goodsId: rows.id,
          shopStockNum: 1
        }
      })
      ElMessage.success('商品添加成功')
      getUserList(route.query.id)
    } catch (error) {
      console.log(error)
      ElMessage.error('商品添加失败')
    }
  }
  // 添加绑定商品
  const addGoodsEvent = () => {
    selectGoodsVisible.value = true
  }

  const editInventoryDialogVisible = ref(false)
  const currentAreaData = ref<any>(null)
  const handleOperation = (row: any) => {
    editInventoryDialogVisible.value = true
    currentAreaData.value = JSON.parse(JSON.stringify(row))
  }
  const handleDialogSuccess = () => {
    editInventoryDialogVisible.value = false
    currentAreaData.value = null
    getUserList(route.query.id)
  }
  const lookProductDetailsVisible = ref(false)
  const productDetails = ref<any>(null)
  const lookGoodsEvent = async (id: any) => {
    try {
      // 调用接口获取详情
      const res = await scoGoodsGetDataUsingPost({
        body: {
          id: id
        }
      })
      lookProductDetailsVisible.value = true
      productDetails.value = res
    } catch (error) {
      console.error('获取详情失败:', error)
      ElMessage.error('获取详情失败')
    }
  }
</script>

<style lang="scss" scoped>
  .actUserDetailBox {
    box-sizing: border-box;
    margin: auto;
  }
</style>
<style lang="scss" scoped>
  .services-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
  }

  .service-tag {
    margin: 2px;
  }

  .no-image {
    font-size: 12px;
    color: rgb(var(--art-text-gray-500));
  }
</style>
